package entity.core.plugins.mycraft.cc;

import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

/**
 * SQL 语句结构
 * 
 * @author delovt
 *
 */
public class SqlComStru {
    private String type  = "";    // 操作类型, 增删查改
    private String db    = "";    // 操作对象
    private String tb    = "";    // 操作对象
    private String field = "";    // 目标需求
    private String where = "";    // 操作条件
    private String limit = "";    // 限制条件
    public boolean flag  = false; // 预编译标识符

    @SuppressWarnings("unused")
    public String GetComm() {
        if (this.type.isEmpty())
            return "";
        if (this.tb.isEmpty())
            return "";
        if (this.field.isEmpty())
            return "";
        String ss = "";
        List<String> flags = new ArrayList<String>();
        flags.add("UPDATE");
        flags.add("INSERT");
        flags.add("DELETE");
        if (flags.contains(this.type.toString()))
            this.flag = true;
        ss = ss + this.type.toString() + " "; // Select
        ss = ss + this.field.toString() + " "; // *
        if (this.db.isEmpty())
            ss = ss + this.tb.toString() + " "; // from
        else
            ss = ss + this.db.toString() + "." + this.tb.toString() + " ";
        if (!this.where.isEmpty()) { // where
            String[] wheres = this.where.split(",");
            StringBuffer st = new StringBuffer();
            for (String s : wheres) {
                String [] t;
                if(s.indexOf("{like}") != -1) {
                    t = s.split("\\{like\\}");
                    st.append(t[0] + " like ? AND ");
                } else if(s.indexOf("{>=}") != -1) {
                    t = s.split("\\{>=\\}");
                    st.append(t[0] + " >= ? AND ");
                } else if(s.indexOf("{<=}") != -1) {
                    t = s.split("\\{<=\\}");
                    st.append(t[0] + " <= ? AND ");
                } else if(s.indexOf("{!=}") != -1) {
                    t = s.split("\\{!=\\}");
                    st.append(t[0] + " != ? AND ");
                }  else if(s.indexOf("{>}") != -1) {
                    t = s.split("\\{>\\}");
                    st.append(t[0] + " > ? AND ");
                } else if(s.indexOf("{<}") != -1) {
                    t = s.split("\\{<\\}");
                    st.append(t[0] + " < ? AND ");
                } else if(s.indexOf("{=}") != -1) {
                    t = s.split("\\{=\\}");
                    st.append(t[0] + " = ? AND ");
                } else {
                    st.append(st);
                }
            }
            this.setWhere(st.toString());
            if (this.type.toLowerCase().equals("select")) {
                ss = ss + "WHERE " + this.where.substring(0, (this.where.length() - 5)).toString() + " ";
                this.flag = true;
            } else if(this.type.toLowerCase().equals("insert into")){
                for (String s : wheres) {
                    st.append("?,");
                }
                this.setWhere(st.toString());
                ss = ss + "VALUES("+this.where.substring(0, (this.where.length() - 1)).toString() + ")";
            } else if(this.type.toLowerCase().equals("update")) {
                ss = ss + "WHERE " + this.where.substring(0, (this.where.length() - 5)).toString() + " ";
            } else if(this.type.toLowerCase().equals("delete from")) {
                this.where = this.where.replaceAll("\\,", " AND");
                ss = ss + this.where.substring(0, (this.where.length() - 5)).toString() + " ";
            } else {
                
            }
        }
        if (this.type.toLowerCase().equals("select")) {
            if (this.limit.isEmpty())
                ss = ss + "LIMIT 0,500 ";
            else
                ss = ss + "LIMIT " + this.limit.toString() + " ";
        }
        // SELECT * FROM test LIMIT 0,500;
        return ss;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDb() {
        return db;
    }

    public void setDb(String db) {
        this.db = db;
    }

    public String getTb() {
        return tb;
    }

    public void setTb(String tb) {
        this.tb = tb;
    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public String getWhere() {
        return where;
    }

    public void setWhere(String where) {
        this.where = where;
    }

    public String getLimit() {
        return limit;
    }

    public void setLimit(String limit) {
        this.limit = limit;
    }
}
